import { Address, toNano, fromNano, } from '@ton/core';
import { Pool } from '../wrappers/Pool';
import { NetworkProvider } from '@ton/blueprint';
import { MyJettonMaster } from '../wrappers/MyJetton_MyJettonMaster';
import { MyJettonWallet } from '../wrappers/MyJetton_MyJettonWallet';

const tspa = "kQCrJ-ArHG3LoX_b9Wz7hLVSNeR0_Cz2hSUwashWDavq-r4P";
const tusdt = "kQCymv2MvB5Bb2Jd45D92HaUfmf06CBm-LJMFU7qjeSjX0V3";

const tspa_owner = "kQBhxrSFHGrvX1g5tubQbuePKvBBMtUMxnUMJRxBKjcTKUFd";
const tusdt_owner = "kQBBO0nvoLTgrW2zWRV_MphUUpcuSlAXpDXxXx0y4NsfB8W7";
const jetton_owner = "0QCjPTREUgXS0n1DxX3-clRKW6NIKp5zF8kxoGkYV2b229sN";

const jetton_to = "0QCSSOrjsJdYJZXPTJbfNQ8wZsoycmWdBagwTzYE3ztmjxQE";

const master_in = tusdt;
const master_out = tspa;
const router = "EQBW4Cnl4ZspBRneKZgbf9CCMnUCvmNyLnq5anEULY4rOWey";

const ownerAddress = Address.parse(jetton_owner);
const masterAAddress = Address.parse(master_in);
const masterBAddress = Address.parse(master_out);

const current_id = 1;
const swap_price = 33_333_000 / 1000;

const pool = "0QDe2tqQhcNs5iP_gd9tXaBxIWrlkmldCkvGb2T4izT4kK_H";

export async function run(provider: NetworkProvider) {
    await getData(provider);
}

async function getData(provider: NetworkProvider) {
    const senderAddress = provider.sender()?.address;
    if (!senderAddress) {
        throw new Error('Sender address is undefined.');
    }
    const poolAddress = Address.parse(pool);
    const myPool = provider.open(
        Pool.fromAddress(poolAddress),
    );
    
    let poolData = await myPool.getPoolData();
    console.log('data:' + poolData.id + "," + poolData.price + ", " + poolData.reverseA);
    
    const masterA = provider.open(
        MyJettonMaster.fromAddress(masterAAddress),
    );
    const masterB = provider.open(
        MyJettonMaster.fromAddress(masterBAddress),
    );
    const routerAddress = Address.parse(router);
    const walletAAddress = await masterA.getGetWalletAddress(routerAddress);
    const walletBAddress = await masterB.getGetWalletAddress(routerAddress);
    const walletA = provider.open(
        MyJettonWallet.fromAddress(walletAAddress),
    );
    const walletB = provider.open(
        MyJettonWallet.fromAddress(walletBAddress),
    );
    const walletAData = await walletA.getGetWalletData();
    const aBal = walletAData.balance;
    console.log('balA:' + aBal/1000000n);
    const walletBData = await walletB.getGetWalletData();
    const bBal = walletBData.balance;
    console.log('balB:' + fromNano(bBal));
}